package cn.com.dingtek.genius.manager.server.config;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;

import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;

@Configuration
public class EventBusConfig {
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor bean = new ThreadPoolTaskExecutor();

        bean.setThreadNamePrefix("ApplicationEventMulticasterExecutor_");
        bean.setCorePoolSize(5);
        bean.setKeepAliveSeconds(200);
        bean.setMaxPoolSize(10);
        bean.setQueueCapacity(20);
        bean.setRejectedExecutionHandler(new DiscardPolicy());
        return bean;
    }


    @Bean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    public ApplicationEventMulticaster applicationEventMulticaster(ConfigurableListableBeanFactory beanFactory) {
        SimpleApplicationEventMulticaster bean = new SimpleApplicationEventMulticaster(beanFactory);
        bean.setTaskExecutor(threadPoolTaskExecutor());
        return bean;
    }
}
